//index.js
//获取应用实例
const app = getApp();
var util = require('../../utils/util');
Page({
  data: {
    PhoneCode: "",
    secound: 0,
    UserInfo_Mobile: "",
    BtnPhoneTxt: "获取验证码"
  },
  onLoad: function (page) {
    var _this = this;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  bindGetUserInfo: function (e) {
    var _this = this;
    wx.login({
      //获取code
      success: function (res) {
        var code = res.code //返回code
        var userinfo = e.detail.userInfo;
        if (userinfo) {
          util.requrest("api/userinfo/getUserOpenID", { //根据code获取openid
            js_code: code
          }, function (data) {
            if (data.type == 1) {
              var littleOpenid = JSON.parse(data.resultdata).openid;
              app.openID = littleOpenid;
              wx.setStorageSync('openID', littleOpenid);
              util.requrest("api/userinfo/loginByOpenID", {
                openid: app.openID
              }, function (result) {
                console.log(result)
                if (result.type == 1) {
                  app.showMsg(result.message);
                  var info = result.resultdata.info;
                  var userInfo = result.resultdata.userInfo;
                  app.globalData.userInfo = info;
                  app.globalData.user = userInfo;

                  if (!!app.shareQid) {
                    wx.reLaunch({
                      url: '../../pages/questionDetail/questionDetail?id=' + app.shareQid
                    })
                  } else {
                    wx.reLaunch({
                      url: '../../pages/index2/index2'
                    })
                  }
                } else {
                  app.isExist = true;
                  app.showMsg("授权成功，请登录。", 0, function(){
                    wx.reLaunch({
                      url: '../../pages/login/login'
                    })
                  })
                  return;
                }
              })
            }else{
              app.showMsg(data.message);
            }
          })
        } else {
          //用户按了拒绝按钮
          wx.showModal({
            title: '警告',
            content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
            showCancel: false,
            confirmText: '返回授权',
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击了“返回授权”')
              }
            }
          })
        }
      }
    })
  },
  btnSendPhoneCode: function () {
    var secound = this.data.secound;
    var _this = this;
    var UserInfo_Mobile = _this.data.UserInfo_Mobile;
    if (UserInfo_Mobile == "") {
      app.showMsg("请输入手机号。");
      return;
    }
    if (UserInfo_Mobile.length != 11){

    }

    if (secound > 0) {
      app.showMsg("请" + secound + "秒后再获取验证码。");
      return;
    }

    util.requrest("api/Home/SendPhoneCode", { UserInfo_Mobile: UserInfo_Mobile }, function (data) {
      if (data.type == 1) {
        app.showMsg(data.message);
        secound = 60;
        var interval = setInterval(function () {
          if (secound <= 0) {
            _this.setData({
              BtnPhoneTxt: "获取验证码"
            })
            clearInterval(interval);
            return;
          }
          _this.setData({
            BtnPhoneTxt: secound + "秒后获取"
          })
          secound--;
          _this.setData({
            secound: secound
          })
        }, 1000);
        return;
      }
      app.showMsg(data.message);
    })
  },
  InputPhoneCode: function (e) {
    var val = e.detail.value;
    this.setData({
      PhoneCode: val
    });
  },
  InputUserInfo_Mobile: function (e) {
    var val = e.detail.value;
    this.setData({
      UserInfo_Mobile: val
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

});

